package edu.faytechcc.csc.gui.search;

import edu.faytechcc.csc.messages.NetworkConnection;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * This Class creates a panel used to search a database over a
 * NetworkConnection. Filtering by genera and title.
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class SearchPanel extends JPanel
{

    private JTextField searchBarTextField;
    private JComboBox generaComboBox;
    private NetworkConnection conn;
    private String table;

    /**
     * Constructor that accepts a NetworkConnection to set the
     * NetworkConnection used in this class to the one input in the
     * constructor.
     *
     * @param conn
     */
    public SearchPanel(NetworkConnection conn)
    {
        this.conn = conn;
        //text bar that will filter results
        searchBarTextField = new JTextField(15);
        searchBarTextField.getDocument().
                addDocumentListener(new SearchBarListener());
        searchBarTextField.addActionListener(new SearchBarEnterListener());
        this.add(searchBarTextField);
        this.AddComboBox();
        this.add(Box.createRigidArea(new Dimension(300,
                0)));

    }

    /**
     * Adds a comboBox to the panel
     */
    private void AddComboBox()
    {
        generaComboBox = new JComboBox();
        try
        {
            File file = new File("Genera_Text.txt");
            Scanner inputFile = new Scanner(file);
            while (inputFile.hasNext())
            {
                String line = inputFile.nextLine();
                if (line != null)
                {
                    generaComboBox.addItem(line.trim());
                }
            }
        } catch (Exception ex)
        {
        }
        generaComboBox.addActionListener(new ComboBoxActionListener());
        this.add(generaComboBox);
    }

    /**
     * Sends message of the genera and title over the NetworkConnection
     */
    private void sendMessage()
    {
        String title = searchBarTextField.getText().toUpperCase();
        String genera = generaComboBox.getSelectedItem().toString().toUpperCase();
        String tableName = this.table;

        conn.sendMessages("SELECT ENG_TITLE, GENERA FROM "
                + tableName
                + " WHERE UPPER(ENG_TITLE) LIKE '%" + title + "%'"
                + " AND UPPER(GENERA) LIKE '%" + genera + "%'"
                + " ORDER BY ENG_TITLE");
    }

    /**
     * @return the table
     */
    public String getTable()
    {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(String table)
    {
        this.table = table;
    }

    /**
     * DocumentListener Class to handle changes to searchBarTextField
     */
    private class SearchBarListener
            implements DocumentListener
    {

        @Override
        public void insertUpdate(DocumentEvent e)
        {
            if (searchBarTextField.getDocument().
                    getLength() >= 3)
            {
                sendMessage();
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            if (searchBarTextField.getDocument().getLength() >= 3)
            {
                sendMessage();
            } else if (searchBarTextField.getDocument().getLength() == 2)
            {
                conn.sendMessages("SELECT ENG_TITLE, GENERA FROM "
                        + table + " ORDER BY ENG_TITLE");
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e)
        {
            //Plain text components do not fire these events
        }
    }

    /**
     * ActionListener Class to handle enter key event
     */
    private class SearchBarEnterListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            sendMessage();
        }
    }

    /**
     *
     */
    private class ComboBoxActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            sendMessage();
        }
    }
}
